#include "Messenger.h"
#include "Constants.h"
#include "Lang.h"
#include "Game.h"

static Uint32 ticks;

#define MESSAGE_TIMEOUT		( 2000 )


static void __clip(void)
{
	SDL_Rect msg_cliprect = { 0, game.viewport.bottom, game.viewport.rect.w, MARGIN };
	SDL_SetClipRect(game.screen, &msg_cliprect);
}

static void __unclip(void)
{
	clip_level();
}

void message_refresh (void)
{
	SDL_Rect rect;

	rect.x = 0;
	rect.y = game.viewport.bottom;
	rect.w = game.viewport.rect.w;
	rect.h = MARGIN;

	SDL_UpdateRects(game.screen, 1, &rect);
}


void message_clear (void)
{

	SDL_Rect src, dst;;

	__clip();
	dst.x = 0;
	dst.y = game.viewport.bottom;
	dst.w = game.viewport.rect.w;
	dst.h = MARGIN;

	src.x = 0;
	src.y = 0;
	src.w = dst.w;
	src.h = dst.h;

	SDL_FillRect (game.screen, &dst, 0);
	SDL_BlitSurface (game.lcd, &src, game.screen, &dst);
	__unclip();
}

int interrupt = 0;

void message_noint (const char * str)
{
	message(str);
	interrupt = 1;	
}

void message (const char * str)
{

	SDL_Rect rect;
	
	printf ("%s\n", str);

	message_clear();

	__clip();
	rect.x = 0;
	rect.y = game.viewport.bottom;
	rect.w = game.viewport.rect.w;
	rect.h = MARGIN;

	SFont_WriteCenter (game.screen, game.font, rect.y + 2, str);
	__unclip();
	
	message_refresh();

	ticks = SDL_GetTicks();

}	

void message_status (void)
{
	
	static char lifes[10];
	static char score[9]; /* Good luck to reach a score of 999.999.999 */
	static char level[16]; 
	
	int clife = -1;
	long cscore = -1;
	int clevel = -1;

	int life_changed = 0;
	int score_changed = 0;
	int level_changed = 0;	
	SDL_Rect rect;

	/* If a non interruptible message is currently displayed,
	   do not replace it */
	if ( interrupt == 1 ) return;
	

	/* Format strings */
	
	if ( game.lifes != clife )
	{	
		if ( game.lifes > 1 )
			sprintf (lifes, STR_CURRENT_LIFE ": %d", game.lifes);
		else
			sprintf (lifes, STR_CURRENT_LIFE ": LAST");

		clife = game.lifes;
		life_changed = 1;
	}

	/* Put dots to make the score more readable */
	if ( game.score != cscore )
	{
		score_sprintf(score, game.score);
		cscore = game.score;
		score_changed = 1;
	}
	
	if ( game.level != clevel )
	{
	 	sprintf (level, STR_LEVEL " %d", game.level);
		clevel = game.level;
		level_changed = 1;
	}

	
	rect.x = 0;
	rect.y = game.viewport.bottom;
	rect.w = game.viewport.rect.w;
	rect.h = MARGIN;

	/* Nothing changed */
	if ( score_changed + life_changed + level_changed == 0 ) return;

	message_clear();
	
	__clip();
	if ( score_changed )
		SFont_WriteCenter (game.screen, game.font, rect.y + 2, score);

	if ( life_changed )
		SFont_Write	  (game.screen, game.font, 3, rect.y + 2, lifes);

	if ( level_changed ) 
		SFont_Write  (game.screen, game.font, rect.w - 16 - 
				SFont_TextWidth(game.font, level), 
				rect.y + 2, level);
	__unclip();

	message_refresh();

}

void messenger_update (void)
{
	Uint32 current_ticks;

	current_ticks = SDL_GetTicks();

	if ( ticks > current_ticks ) return;

	if ( current_ticks - ticks >= MESSAGE_TIMEOUT )
	{

		interrupt = 0;
		/* Print the status message (lifes remaining, score, level number) */
		message_status();

		/* Avoid blitting at each call to update when there's no message. */
		ticks = 999999999;
	}
}

